#include "Common.h"
bool MakeFifo()
{
    int n = mkfifo(FILENAME, 0666);
    if (n < 0)
    {
        cerr << "errno:" << errno << ",errstring" << strerror(errno) << endl;
        return 0;
    }
    cout << "mkfifo success..." << endl;
    return 1;
}
int main()
{

    int rfd = open(FILENAME, O_RDONLY);
    if (rfd < 0)
    {
        cerr << "errno" << errno << ",ErrnoString:" << strerror(errno) << endl;
        if (MakeFifo())
        {
            cout << "The last Fifo not exist is solved\n";
            rfd = open(FILENAME, O_RDONLY);
        }
        else
            return 1;
    }
    cout << "open fifo success..." << endl;
    while (1)
    {
        char buffer[1024];
        ssize_t s = read(rfd, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            cout << "Client Say:" << buffer << endl;
            buffer[s] = 0;
        }
        // else if (s == 0)
        // {
        // }
        // else
        // {
        // }
    }
    close(rfd);
    cout << "close fifo success..." << endl;
    return 0;
}